package com.faxsun.core.catalog.dao;

import javax.annotation.Resource;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;

import org.broadleafcommerce.common.persistence.EntityConfiguration;
import org.broadleafcommerce.core.catalog.domain.ProductAttribute;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;

@Repository("fsProductAttributeDao")
public class FSProductAttributeDaoImpl implements FSProductAttributeDao {

	@PersistenceContext(unitName = "blPU")
	protected EntityManager em;

	@Resource(name = "blEntityConfiguration")
	protected EntityConfiguration entityConfiguration;

	@Override
	public ProductAttribute create() {
		return (ProductAttribute) entityConfiguration.createEntityInstance(ProductAttribute.class.getName());
	}

	@Override
	public ProductAttribute save(ProductAttribute productAttribute) {
		return em.merge(productAttribute);
	}

	@Override
	public void delete(ProductAttribute productAttribute) {

		if (!em.contains(productAttribute)) {
			productAttribute = readAttributeById(productAttribute.getId());
		}
		em.remove(productAttribute);

	}

	@Override
	public ProductAttribute readAttributeById(long productId) {
		return em.find(ProductAttribute.class, productId);
	}

}
